import {Page} from '@/base/Page';
import {Banners} from '@/base/model/Banners';
import {getBannersList, deleteBanners, PutBannerId} from '@/base/api/BannersApi';

class Form {
  // createBegin = '';
  // createEnd = '';
  bannername = '';
  companyname = '';
  agentname = '';
  companyid= 0;
  position = 1;
  // date = [];
  status = 1;
}

export class ListBloc {
  form = new Form();
  paged: Page<Banners> = new Page<Banners>([], 0, 1, 15, 0, []);
  loading = false;
  type: string | undefined;

  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    return getBannersList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      position: this.form.position,
      bannername: this.form.bannername,
      companyname: this.form.companyname,
      agentname: this.form.agentname
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }
  removeItem(dateItem: Banners) {
    this.loading = true;
    return deleteBanners(dateItem.bannerid)
      .finally(() => {
        this.loading = false;
      });
  }

  switchStatus(id, value, index) {
    if (value) {
      this.form.status = 1
    } else {
      this.form.status = 0
    }
    this.modifyItem(id, this.form.status)

  }

  modifyItem(
             bannerId: number,
             status: number
             ) {
    this.loading = true;
    return PutBannerId({
      bannerId,
      status
    }).then((res) => {
      // deepAssign(this.form, res.data);
      this.load(this.paged.pageIndex, this.paged.pageSize);
      // return res;
    }).finally(() => {
      this.loading = false;
    })
  }
}
